package com.cndongtai.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.cndongtai.dao.BaseDao;
import com.cndongtai.dao.inter.DtuMapper;
import com.cndongtai.dao.inter.LightMapper;
import com.cndongtai.po.Dtu;

@Repository("dtuDao")
public class DtuDao extends BaseDao implements DtuMapper{

	@Override
	public boolean addDtu(Dtu dtu) {
		SqlSession sqlSession = getSqlSession();
		try {
			DtuMapper operation = sqlSession.getMapper(DtuMapper.class);
			return operation.addDtu(dtu);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return false;
	}

	@Override
	public List<Dtu> queryDtuList() {
		
		SqlSession sqlSession = getSqlSession();
		try {
			DtuMapper operation = sqlSession.getMapper(DtuMapper.class);
			return operation.queryDtuList();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return null;
	}

	@Override
	public boolean delDtu(Integer dtuId) {
		SqlSession sqlSession = getSqlSession(false);
		try {
			DtuMapper operDtu = sqlSession.getMapper(DtuMapper.class);
			boolean res = operDtu.delDtu(dtuId);
			
			if(res) {
				LightMapper operLight = sqlSession.getMapper(LightMapper.class);
				res = operLight.delLightByDtuId(dtuId);
				sqlSession.commit();
				return true;
			}
			
			sqlSession.rollback();
		}catch(Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			return false;
		}finally {
			sqlSession.close();
		}
		
		return false;
	}

	@Override
	public Dtu queryDtuById(Integer dtuId) {
		SqlSession sqlSession = getSqlSession();
		try {
			DtuMapper operation = sqlSession.getMapper(DtuMapper.class);
			return operation.queryDtuById(dtuId);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return null;
	}

	@Override
	public Dtu queryDtuByPanId(String panId) {
		SqlSession sqlSession = getSqlSession();
		try {
			DtuMapper operation = sqlSession.getMapper(DtuMapper.class);
			return operation.queryDtuByPanId(panId);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return null;
	}

	@Override
	public int changeLightState(Long dtuId, Integer lightState) {
		SqlSession sqlSession = getSqlSession(false);
		try {
			DtuMapper operDtu = sqlSession.getMapper(DtuMapper.class);
			int resDtu = operDtu.changeLightState(dtuId, lightState);
			
			if(resDtu>0) {
				LightMapper operLight = sqlSession.getMapper(LightMapper.class);
				int resLight = operLight.changeLightStateByDtuId(dtuId, lightState);
				if(resLight > 0) {
					sqlSession.commit();
					return resLight;
				}
			}
			sqlSession.rollback();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return -1;
	}

	@Override
	public List<Dtu> queryDtuListByUserId(Long userId) {
		
		SqlSession sqlSession = getSqlSession();
		try {
			DtuMapper operation = sqlSession.getMapper(DtuMapper.class);
			return operation.queryDtuListByUserId(userId);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return null;
	}

	@Override
	public int changeOnlineState(Long dtuId, Integer onlineState) {
		SqlSession sqlSession = getSqlSession(false);
		try {
			DtuMapper operDtu = sqlSession.getMapper(DtuMapper.class);
			int resDtu = operDtu.changeLightState(dtuId, onlineState);
			
			if(resDtu>0) {
				LightMapper operLight = sqlSession.getMapper(LightMapper.class);
				int resLight = operLight.changeOnlineStateByDtuId(dtuId, onlineState);
				if(resLight > 0) {
					sqlSession.commit();
					return resLight;
				}
			}
			sqlSession.rollback();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return -1;
	}

	@Override
	public boolean updateDtuLocation(Dtu dtu) {
		SqlSession sqlSession = getSqlSession();
		try {
			DtuMapper operation = sqlSession.getMapper(DtuMapper.class);
			return operation.updateDtuLocation(dtu);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return false;
	}

	@Override
	public boolean changePanId(Long dtuId, String panId) {
		SqlSession sqlSession = getSqlSession();
		try {
			DtuMapper operation = sqlSession.getMapper(DtuMapper.class);
			return operation.changePanId(dtuId, panId);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return false;
	}

}
